package dao;

import entity.FilmInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import tool.MyBatisUtil;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class FilmInfoDaoImpl implements FilmInfoDao{
    //mybatis 操作数据库步骤3个核心类
    SqlSessionFactoryBuilder builder = null;
    SqlSessionFactory factory = null;//SqlSessionFactory由SqlSessionFactoryBuilder构建
    SqlSession sqlSession = null;//真正操作数据库的核心对象 执行增删改 SqlSession由SqlSessionFactory构建
    //输入流读取配置文件
    Reader reader = null;


    @Override
    public List<FilmInfo> getFilmInfos() {
        List<FilmInfo> list = new ArrayList<>();
        try{
            //读取配置文件
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            // 构建SqlSessionFactoryBuilder
            builder = new SqlSessionFactoryBuilder();
            // 构建SqlSessionFactory
            factory = builder.build(reader);
            // 构建SqlSession
            sqlSession = factory.openSession();
            //执行数据库的操作dao接口的全限定名称+id名
           // list = sqlSession.selectList("dao.FilmInfoDao.findfilminfo");//需要解析dao接口的配置文件
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfos();
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }

    }

    @Override
    public List<FilmInfo> getFilmInfosByname(String filmname) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            //读取配置文件
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            // 构建SqlSessionFactoryBuilder
            builder = new SqlSessionFactoryBuilder();
            // 构建SqlSessionFactory
            factory = builder.build(reader);
            // 构建SqlSession
            sqlSession = factory.openSession();
            //执行数据库的操作dao接口的全限定名称+id名
            // list = sqlSession.selectList("dao.FilmInfoDao.findfilminfo");//需要解析dao接口的配置文件
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByname(filmname);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }

    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition(String filmname, Integer money) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            // 构建SqlSession
            sqlSession = MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名称+id名
            // list = sqlSession.selectList("dao.FilmInfoDao.findfilminfo");//需要解析dao接口的配置文件
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition(filmname,money);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition2(Map<String, Object> map) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition2(map);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition3(FilmInfo filmInfo) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition3(filmInfo);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition4(FilmInfo filmInfo) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition4(filmInfo);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition5(String choice) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition5(choice);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition6(Integer[] typeid) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition6(typeid);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition7(List<Integer> typeid) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition7(typeid);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }

    }

    @Override
    public List<FilmInfo> getFilmInfosAndType() {
        List<FilmInfo> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosAndType();
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int getFilmCount() {
        int count = 0;
        try{
            sqlSession = MyBatisUtil.getSession();
            count = sqlSession.getMapper(FilmInfoDao.class).getFilmCount();
            return count;

        }catch (Exception e){
            e.printStackTrace();
            return -1;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosAndTypeByPage(Integer pageIndex, Integer pageSize) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosAndTypeByPage(pageIndex,pageSize);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int getFilmCount2(FilmInfo filmInfo) {
        int count = 0;
        try{
            sqlSession = MyBatisUtil.getSession();
            count = sqlSession.getMapper(FilmInfoDao.class).getFilmCount2(filmInfo);
            return count;

        }catch (Exception e){
            e.printStackTrace();
            return -1;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosAndTypeByPage2(Integer pageIndex, Integer pageSize, FilmInfo filmInfo) {
        List<FilmInfo> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.getSession();
            list = sqlSession.getMapper(FilmInfoDao.class).getFilmInfosAndTypeByPage2(pageIndex,pageSize,filmInfo);
            return list;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int delFilminfo(Integer filmid) {
        int count = 0;
        try{
            sqlSession = MyBatisUtil.getSession();
            count = sqlSession.getMapper(FilmInfoDao.class).delFilminfo(filmid);
            sqlSession.commit();//事务提交 增删改要事务提交
            return count;

        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return -1;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int addFilminfo(FilmInfo filmInfo) {
        int count = 0;
        try{
            sqlSession = MyBatisUtil.getSession();
            count = sqlSession.getMapper(FilmInfoDao.class).addFilminfo(filmInfo);
            sqlSession.commit();//事务提交 增删改要事务提交
            return count;

        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return -1;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int updatFilminfo(FilmInfo filmInfo) {
        int count = 0;
        try{
            sqlSession = MyBatisUtil.getSession();
            count = sqlSession.getMapper(FilmInfoDao.class).updatFilminfo(filmInfo);
            sqlSession.commit();//事务提交 增删改要事务提交
            return count;

        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return -1;

        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }


}
